<?php
/**
 * @package contenite
 */
?><div id="loading_div<?php echo isset($indicator) ? "_{$indicator}" : ''; ?>" style="display: none;">
  <?php echo $html->image('ajax-loader.gif'); ?>
</div>
<table>
<?php
$opt = array();
echo '<tr>';

if($this->name == 'Uam' && isset($indicator) && $indicator == 'regusers') {
  echo '<th>Selected</th>';
}

//D echo "s=", $sortby, ", d=", $direction;
foreach ($head as $name => $title) {
  if (is_array($title)) {
		$opt[$name]['format'] = isset($title['format']) ? $title['format'] : 'text'; 
		$opt[$name]['css'] = isset($title['css']) ? $title['css'] : ''; 
		$opt[$name]['type'] = isset($title['type']) ? $title['type'] : 'text';
		$opt[$name]['id'] = isset($title['id']) ? $title['id'] : '';
		$opt[$name]['explain'] = isset($title['explain']) ? $title['explain'] : '';
    $title = $title[0];
    if ($opt[$name]['type'] == 'info') {
      // no header. must be last field
      break;
    }
  }
  else {
	  $opt[$name]['format'] = 'text';
	  $opt[$name]['css'] = '';
	  $opt[$name]['type'] = 'text';
		$opt[$name]['id'] = '';
		$opt[$name]['explain'] = '';
  }
  $attr = array();
  if (isset($paginator)) {
    $what = isset($what) ? $what : '';
    
    if (isset($sortby) && $name == $sortby && isset($direction)) {
      $attr[] = 'class="' . $direction . '"';
    }

    // PaginatorHelper needs short names as sort_key
    $column_name = preg_replace('/:.*$/', '', $name);
    $sort_key = $paginator instanceof PaginatorHelper 
      ? preg_replace('/[^\.]+\./', '', $column_name) : $column_name;
    if($this->name == 'Uam' && isset($indicator) && $indicator == 'regusers') {
      $dir = ($sortby == $name && $direction == 'asc') ? 'desc' : 'asc';
      if($name != 'Userlist.cntVotes' && $name != 'Userlist.cntLinkedStories' && $name != 'Userlist.cntWrittenStories' && $name != 'Userlist.cntPinboardEntries' && $name != 'Userlist.last_action') {
        echo '<th><a href="#" onclick="reg_users_filter(' . $paginator->current() . ', \'' . $name . '\', \'' . $dir . '\');">' . $title . '</a></th>';
      }
      else {
        echo '<th>' . $title . '</th>';
      }
    }
    else {
      echo '<th n=' . $sort_key . ' ' . join(' ', $attr) . '>', $paginator->sort($title, $sort_key, array(
        'direction' => $direction,
        'what' => $what)), '</th>';
    }
  }
  elseif($this->name == 'Statistics') {
    echo '<th style="vertical-align: bottom;">'. $title. '</th>';
  }
  else {
    echo '<th>'. $title. '</th>';
  }
}

if($this->name == 'Uam' && isset($indicator) && $indicator == 'regusers') {
  echo '<th>History</th>';
}
if($this->name == 'Feeds' && isset($indicator) && $indicator == 'feed_stories') {
  echo '<th>Actions</th>';
}
echo "</tr>\n";

$last = count($data) - 1;
foreach ($data as $i => $row) {
	$class = $i % 2 ? 'even' : 'odd';
	$class .= $i == 0 ? ' first' : '';
	$class .= $i == $last ? ' last' : '';
	echo '<tr class="', $class, '">';

  if($this->name == 'Uam' && isset($indicator) && $indicator == 'regusers') {
    echo '<td style="text-align: center;"><input type="checkbox" name="sendtouser" id="sendtouser_' . $row['Userlist']['userID'] . '" value="' . $row['Userlist']['userID'] . '" onchange="select_user(' . $row['Userlist']['userID'] . ', \'' . $row['Userlist']['name'] . '\');" /><script type="text/javascript">is_selected_user(' . $row['Userlist']['userID'] . ');</script></td>';
  }

  foreach (array_keys($head) as $path_name) {
	  // path looks like 'Locale.localeID' with optional suffix ':xxx'
    $column_name = preg_replace('/:.*$/', '', $path_name);
	  
	  $value = flatten($row, $column_name);
	  
    $attr = array(); 
    if ($opt[$path_name]['explain']) {
      $attr[] = 'title="' . addslashes($opt[$path_name]['explain']) . '"';
    }
    switch ($opt[$path_name]['type']) {
      case 'checked':
        $checked = $value && $value != '0000-00-00 00:00:00';
        $value = '<span title="' . ($checked ? $value : '') . '">' 
          . ($checked ? '[x]' : '[ ]') . '</span>';
        break;
      case 'timestamp':
        if (is_numeric($value)) {
          $value = $value ? strftime($opt[$path_name]['format'], $value) : '';
        }
        else {
          $value = $value && $value != '0000-00-00 00:00:00' ? $value : '';
        }
        break;
      case 'url':
      /** does not work for both Feed.url and url
        $value = '<a href="' . $value . '" target="_new">'
          . eval('return ' . $opt[$path_name]['format'] . ';') . '</a>';
      **/
        $title = preg_replace('#^http://(www\.)?#', '', $value);
        if (strlen($title) > 50) {
          $url = parse_url($value);
          $title = preg_replace('#^www\.#', '', $url['host']) . '/...' . substr($url['path'], -40);
        }
        $value = sprintf('<a href="%s">%s</a>', $value, $title);
        break;
      case 'select':
        $value = '<img src="/img/pixel.gif" width="16" height="16" />';
        
        $id = flatten($row, $opt[$path_name]['id']);
        $attr[] = 'table:id="' . $id . '"';
        break;
      case 'feed_stories_link':
        $value = '<a href="' . $value . '">Klick</a>';
        break;
      default:
        if ($opt[$path_name]['format'] != 'text') {
          $value = sprintf($opt[$path_name]['format'], $value);
        }
		}

    if ($opt[$path_name]['type'] == 'info') {
      if ($value) {
        // plain text info in last field is put on separate line
        echo '</tr><tr><td colspan="2">&nbsp;</td><td colspan="', count($head) -2, '">', 
          $value, '</td>';
      }
    }
    else {
  		$class = as_css($path_name);
  		$class .= $opt[$path_name]['css'] ? (' ' . $opt[$path_name]['css']) : '';
  	  echo '<td table:field="' . addslashes($path_name)
  	    . '" ' . join(' ', $attr) . ' class="' . $class . '">', $value, '</td>';
    }
	}

  if($this->name == 'Uam' && isset($indicator) && $indicator == 'regusers') {
    echo '<td style="text-align: center;"><a href="#" onclick="showHistory(' . $row['Userlist']['userID'] . '); jQuery(\'#uam_history\').toggle();">View</a></td>';
  }
  if($this->name == 'Feeds' && isset($indicator) && $indicator == 'feed_stories') {
    echo '<td style="text-align: center;"><a href="/feeds/deletefeedstory/' . $row['Stories']['feedStoryID'] . '">Delete</a></td>';
  }

	echo "</tr>\n";
}
?>
</table>
<?php
if (isset($paginator) && !isset($nolinks)) {
  echo $paginator->prev('<< Previous ', null, null, array('class' => 'disabled'));
  echo $paginator->next(' Next >>', null, null, array('class' => 'disabled'));
}
?>